using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using HTML;

namespace PointBridge.SPDX
{
    public class OptionLine
    {
        #region Constructors
        public OptionLine(string p_optionName, bool p_optionValue, long p_order)
        {
            optionName = p_optionName;
            optionValue = p_optionValue;
            order = p_order;
            htmlColor = "#5A595A";
        }
        public OptionLine(string p_optionName, bool p_optionValue, long p_order, Color p_color)
        {
            optionName = p_optionName;
            optionValue = p_optionValue;
            order = p_order;
            color = p_color;
        }
        public OptionLine(OptionLine obj)
        {
            CopyFrom(obj);
        }
        public OptionLine(LAHTMLParser parser)
        {
            ParseXML(parser);
        }
        #endregion

        #region Methods
        public void CopyFrom(OptionLine obj)
        {
            optionName = obj.optionName;
            optionValue = obj.optionValue;
            order = obj.order;
            htmlColor = obj.htmlColor;
        }
        #endregion

        #region XML Methods
        public void ParseXML(LAHTMLParser parser)
        {
            AttributeList tag = parser.ProcessTag("OptionLine");
            ReadAttributeList(tag);
        }
        public string ToXMLString()
        {
            return ToAttributeList().ToXMLString(true);
        }
        private AttributeList ToAttributeList()
        {
            AttributeList tag = new AttributeList();
            tag.Name = "OptionLine";
            tag.AddXMLAttribute("_ver", _ver);
            tag.AddXMLAttribute("_optionName", _optionName);
            tag.AddXMLAttribute("_optionValue", _optionValue);
            tag.AddXMLAttribute("_order", _order);
            tag.AddXMLAttribute("_htmlColor", htmlColor);
            return tag;
        }
        private void ReadAttributeList(AttributeList tag)
        {
            if (tag.Name != "OptionLine") throw new PointBridgeEx("Trying to read OptionLine from where tag = " + tag.Name);
            int ver;
            tag.ReadXMLAttribute("_ver", out ver);
            if (ver > _ver) throw new PointBridgeEx("Trying to read OptionLine version=" + ver + " while class version=" + _ver);

            tag.ReadXMLAttribute("_optionName", out _optionName);
            tag.ReadXMLAttribute("_optionValue", out _optionValue);
            if (ver >= 2)
            {
                tag.ReadXMLAttribute("_order", out _order);
                string tmp; tag.ReadXMLAttribute("_htmlColor", out tmp); htmlColor = tmp;
            }
            else
            {
                _order = 0;
                htmlColor = "#5A595A";
            }
        }
        #endregion

        #region Accessors & Mutators
        public string optionName
        {
            get
            {
                return _optionName;
            }
            set
            {
                _optionName = value;
            }
        }
        public bool optionValue
        {
            get
            {
                return _optionValue;
            }
            set
            {
                _optionValue = value;
            }
        }
        public long order
        {
            get
            {
                return _order;
            }
            set
            {
                _order = value;
            }
        }
        public Color color
        {
            get
            {
                return _color;
            }
            set
            {
                _color = value;
            }
        }
        public string htmlColor
        {
            get
            {
                return ColorTranslator.ToHtml(_color);
            }
            set
            {
                _color = ColorTranslator.FromHtml(value);
            }
        }
        #endregion

        #region Internal fields
        private const int _ver = 2;

        private string _optionName;
        private bool _optionValue;
        private long _order;
        private Color _color;
        #endregion
    }
}
